import time

import orm


def show_product():  # 显示商品清单
    sql = f"""
    select num,name,price,discount from products
"""
    data = orm.read_data(sql)
    if data != ():
        print(f'----编码----名称----价格----折扣----')
        for goods in data:
            print(f"{goods[0]}\t{goods[1]}\t\t{goods[2]}\t\t{goods[3]}")


def get_product():
    while True:
        data = ()
        print('选择查询的方式：1商品编号2商品名称3商品编号和商品名称4退出查询')
        choice = int(input("选择操作："))
        if choice == 1:
            num = int(input("请输入您要查询的商品编号："))
            sql = f"""
                select * from products where num={num}
"""
            data = orm.read_data(sql)
        elif choice == 2:
            name = input("请输入您要查询的商品名称：")
            sql = f"""
                select * from products where name={name}
"""
            data = orm.read_data(sql)
        elif choice == 3:
            sql = f"""
                select * from products where num={num} and name={name}
"""
            data = orm.read_data(sql)
        elif choice == 4:
            print('正在退出......')
            break
        else:
            print('请输入正确的查询方式！！！！！')

        if data != ():
            print('----编码----名称----价格----折扣----')
            for goods in data:
                print(f"{goods[0]}\t{goods[1]}\t\t{goods[2]}\t\t{goods[3]}")
        else:
            print("商品不存在")
            time.sleep(1)


def add_product():
    num = int(input("请您创建商品编号："))
    name = input("请输入您要添加的商品名称：")
    price = float(input("请设置商品的价格（元）："))
    discount = float(input("请设置商品的折扣（元）："))
    tag = 0
    sql = f"""insert into products values(null,{num},'{name}',{price},{discount})"""
    rows = orm.write_data(sql)
    print(rows)
    if rows > 0:
        tag = 1
    return tag


def mod_product():
    print('*-' * 20)
    while True:
        num = int(input('请输入需要修改的商品编号：'))
        search_sql=f"""select * from products where num={num}"""
        s_data=orm.read_data(search_sql)
        for goods in s_data:
            print(f"{goods[0]}\t{goods[1]}\t\t{goods[2]}\t\t{goods[3]}")

        print('1商品编号2商品名称3商品价格4商品折扣5退出修改')
        chioce = int(input('请选择需要修改的商品属性:'))


        if chioce == 1:
            sql = f"""update products set num = '{num}' where num = '{num}' """
        elif chioce == 2:
            name = input("请输入您要修改的商品名称：")
            sql = f"""update products set name = '{name}' where num = '{num}' """
        elif chioce == 3:
            price = float(input("请修改商品的价格（元）："))
            sql = f"""update products set price = '{price}' where num = '{num}' """
        elif chioce == 4:
            discount = float(input("请修改商品的折扣（元）："))
            sql = f"""update products set discount = '{discount}' where name = '{num}' """

        elif chioce == 5:
            print('正在退出......')
            break
        else:
            print('请输入正确的操作码：')
        data=orm.write_data(sql)

        if data != ():
            print('----编码----名称----价格----折扣----')
            for goods in data:
                print(f"{goods[0]}\t{goods[1]}\t\t{goods[2]}\t\t{goods[3]}")


def del_product():
    pass


def show_orders():
    pass


def del_order():
    pass


def count_orders():
    pass


def add_order():
    amount = 0
    count = 0
    while True:
        data = get_product()
        if data != ():
            num = int(input('请输入要购买的数量：'))
            money = data[0][2] * num
            count += num
            amount += money
            choice = int(input('请输入你的选择1继续2退出：'))
            if choice == 1:
                continue
            else:
                break
    print(f'总数量{count}总金额{amount}')
    sql = f"""
        select max(num) from orders
    """
    data = orm.read_data(sql)
    id = data[0][0] + 1
    sql = f"""
        insert into orders values(null,{id},{count},{amount})
    """
    rows = orm.write_data(sql)
    if rows > 0:
        print('订单写入成功')
    else:
        print('订单写入失败')


if __name__ == '__main__':
    # 测试一下
    # show_product()
    # 查询
    # get_product()
    # 添加商品
    # add_product()
    #修改商品
    mod_product()